<?php
namespace app\common\model;
class Daily extends Base
{
    protected function initialize()
    {
        parent::initialize();
    }

    /* 增加字段 */
    public static function inc($field,$where=false,$number=1) {
        if(!$where) {
            $where = [
                'create_date' => date('Y-m-d')
            ];
        }
        self::where($where)->setInc($field,$number);
    }

    /* 统计一次数据 */
    public static function tongji () {
        //订单金额
        //订单商品数
        $date = date('Y-m-d');
        $log = self::getOne([
            'create_date' => $date
        ]);
        if(!$log) {
            self::create([
                'create_date' => $date
            ]);
        }
        $data = [
            'user_all' => User::getCount(1), //总用户数
            'grade1_all' => User::getCount(['grade_id'=>1]),
            'grade2_all' => User::getCount(['grade_id'=>2]),//vip总数
            'grade3_all' => User::getCount(['grade_id'=>3]), //加盟商总数
            'grade4_all' => User::getCount(['grade_id'=>4]),//总代总数
            'uv' => Loginstatus::getCount([
                'update_time' => [
                    'gt' , strtotime($date)
                ]
            ]),//uv
            'order_number' => Order::getCount([
                'pay_time' => [
                    'gt' , strtotime($date)
                ],
                'checked' => [
                    'in',[1,2,5]
                ],
                'shop_type' => 1
            ]),//订单数
            'order_score_number' => Order::getCount([
                'pay_time' => [
                    'gt' , strtotime($date)
                ],
                'checked' => [
                    'in',[1,2,5]
                ],
                'shop_type' => 2
            ]),//积分商城订单数
            'order_money' => Order::where([
                'pay_time' => [
                    'gt' , strtotime($date)
                ],
                'checked' => [
                    'in',[1,2,5]
                ],
                'shop_type' => 1
            ])->sum('all_money'),//订单金额
            'order_user' => count(Order::where([
                'pay_time' => [
                    'gt' , strtotime($date)
                ],
                'checked' => [
                    'in',[1,2,5]
                ],
                'shop_type' => 1
            ])->group('uid')->field('uid')->select()->toArray()),//去重下单人数
        ];
        //转化率
        $data['order_trans_rate'] = round($data['uv']==0?0:($data['order_user']/$data['uv']),3);
        //客单价
        $data['order_per_money'] = round($data['order_user']==0?0:($data['order_money']/$data['order_user']),3);

        //商品数
        $orderArr = Order::where([
            'pay_time' => [
                'gt' , strtotime($date)
            ],
            'checked' => [
                'in',[1,2,5]
            ],
            'shop_type' => 1
        ])->column('id');
        $data['order_goods'] = OrderGoods::where([
            'order_id' => [
                'in',$orderArr?$orderArr:[0]
            ]
        ])->sum('goods_num');

        Daily::where([
            'create_date' => $date
        ])->update($data);
    }
}